package com.etc.demo1.demo.controller;


import com.etc.demo1.demo.domain.Order;
import com.etc.demo1.demo.service.OrderService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderController {
    private OrderService orderService;

    //查询全部
    @RequestMapping("/list")
    public String goodsList(Model model){
        List<Order> orders=orderService.findAll();
        model.addAttribute("orders",orders);
        return "index";
    }

    //新增数据
    @RequestMapping("/add")
    public String save(Order orders){
        orderService.save(orders);
        return "redirect:/list";
    }

    //删除数据
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException {
        int count=orderService.delete(id);
        if (count==1){
            servletResponse.sendRedirect("/list");
        }
        return "error";
    }

    //根据id查找
    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model,@PathVariable int id){
        Order orders=orderService.getOrderById(id);
        model.addAttribute("orders",orders);
        return "modifi";
    }

    //更新数据
    @PostMapping("/update")
    public String updateUser(Model model, Order orders, HttpServletRequest request){
        String id=request.getParameter("id");
        Order userByid=orderService.getOrderById(Integer.parseInt(id));
        orderService.update(orders);
        System.out.println(orders);
        return "redirect:/list";
    }
}
